﻿@{
    ViewBag.Title = "ApiDoc";
    Layout = "~/Views/Share/_LayoutApiDoc.cshtml";
}

@section css{
    <link href="/Content/noke/js/plugins/editormd/editormd.css" rel="stylesheet" />
}


    <div class="row" style="margin:3px;">
        <div class="col-lg-12" id="addApidoc">
            <div class="row">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>新增文档</h5>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal">
                            <div class="form-group"><label class="col-lg-2 control-label">文档名称</label>
                                <div class="col-lg-10"><input id="txtApiName" type="text" placeholder="" class="form-control"></div>
                            </div>
                            <div class="form-group">
                                <div class="col-lg-offset-2 col-lg-10">
                                    <button class="btn btn-sm btn-white" type="submit" onclick="saveApi();"><i class="fa fa-save"></i> Save</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-12" id="editortoolbar">
            <div class="row">
                <div class="col-lg-2">
                    <input id="txtparentno" class="form-control" type="text" disabled/>
                </div>
                <div class="col-lg-2">
                    <input id="txtno" class="form-control" type="text" disabled/>
                </div>
                <div class="col-lg-4">
                    <input id="txtlabel" class="form-control" type="text" style="width:280px;" />
                    <div style="display:none;">
                        <textarea class="form-control" rows="3" id="txtremark"></textarea>
                        <input type="text" class="form-control" id="txttype" placeholder="" />
                        <input type="text" class="form-control" id="txtlevel" placeholder="" />
                        <input type="text" class="form-control" id="txtordernum" placeholder="" />
                        <textarea class="form-control" rows="3" id="txtv1"></textarea>
                        <textarea class="form-control" rows="3" id="txtv2"></textarea>
                        <textarea class="form-control" rows="3" id="txtv3"></textarea>
                    </div>
                </div>
                <div class="col-lg-4" style="margin:3px 0px;">
                    <div data-toggle="buttons-checkbox" class="btn-group">
                        <button class="btn btn-info" type="button" onclick="loadTemplate();"><i class="fa fa-save"></i> 加载模板</button>
                        <button class="btn btn-info" type="button" onclick="save();"><i class="fa fa-save"></i> 保存</button>
                        <button class="btn btn-info" type="button" onclick="publish();"><i class="fa fa-yelp"></i> 发布成 HTML 文件</button>
                    </div>
                    <a id="a-preview" href="javascript:void(0);" class="btn btn-success" style="display:none;" target="_blank"><i class="fa fa-search"></i> 预览</a>


                </div>
            </div>
            <div id="test-editormd">
                <textarea style="display:none;"></textarea>
            </div>
        </div>

    </div>
    
<!-- 模板存放的地方 -->
    <div id="api-doc-templ" style="display:none">

**简要描述：** 用户注册接口

**请求URL：** ` http://xx.com/api/user/register `
  
**请求方式：** - POST 

**参数：** 

|参数名|必选|类型|说明|
|:----    |:---|:----- |-----   |
|username |是  |string |用户名   |
|password |是  |string | 密码    |
|name     |否  |string | 昵称    |

 **返回示例**

``` 
  {
    "error_code": 0,
    "data": {
      "uid": "1",
      "username": "12154545",
      "name": "吴系挂",
      "groupid": 2 ,
      "reg_time": "1436864169",
      "last_login_time": "0",
    }
  }
```

 **返回参数说明** 

|参数名|类型|说明|
|:-----  |:-----|-----                           |
|groupid |int   |用户组id，1：超级管理员；2：普通用户  |

 **备注** 

- 更多返回错误代码请看首页的错误代码描述

    </div>

@section scripts{
    <script src="~/Content/noke/js/plugins/editormd/editormd.min.js"></script>
    <script>
        var testEditor = null;
        $(document).ready(function () {
            var menu = new fformmenu();// 初始化导航菜单

            if (menu.menuno == null) {
                $('#editortoolbar').hide();
                $('#addApidoc').show();
                return;
            }
            $('#addApidoc').hide();
            $('#editortoolbar').show();


            testEditor = editormd("test-editormd", {
                height: 800,
                syncScrolling: "single",
                path: '/Content/noke/js/plugins/editormd/lib/'
            });


            // 延迟1s，等待EditorMarkdown控件初始化完成
            setTimeout("load(" + menu.menuno + ")", 1000);

            //load(menu.menuno);

        });

        function load(no) {
            var url = '/Home/GetApiDoc/?no=' + no;
            $.getJSON(url, function (model) {
                if (model) {
                    $('#txtno').val(model.no);
                    $('#txtlevel').val(model.level);
                    $('#txtordernum').val(model.ordernum);
                    $('#txtparentno').val(model.parentno);
                    $('#txttype').val(model.type);
                    $('#txtlabel').val(model.label);
                    $('#txtremark').val(model.remark);
                    $('#txtv1').val(model.v1);
                    $('#txtv2').val(model.v2);
                    $('#txtv3').val(model.v3);
                    //alert(model.v3);
                    testEditor.insertValue(model.v3);

                    $('#a-preview').attr('href', '/content/document/api/' + model.no + '.html').show();
                } else {
                    console.log('Get:' + res.Msg);
                }
            }, 'json');
        }

        function loadTemplate() {

            $('#txtno').val('');
            //$('#txtlevel').val(model.level);
            $('#txtordernum').val(999999);
            //$('#txtparentno').val(model.parentno);
            //$('#txttype').val(model.type);
            $('#txtlabel').val('');
            $('#txtremark').val('');
            $('#txtv1').val('');
            $('#txtv2').val('');
            $('#txtv3').val('');

            var tmpl = $("#api-doc-templ").html();
            testEditor.setValue(tmpl);
            $('#a-preview').hide();
        }

        function save() {

            var model = {};
            model.no = $('#txtno').val();
            model.level = parseInt($('#txtlevel').val());
            model.ordernum = parseInt($('#txtordernum').val());
            model.parentno = $('#txtparentno').val();
            model.type = parseInt($('#txttype').val());
            model.label = $('#txtlabel').val();
            model.remark = $('#txtremark').val();
            model.v1 = '';
            model.v2 = $('#txtv2').val();
            model.v3 = testEditor.getMarkdown();

            if (model.no == '') {
                model.no = (new Date().getTime()).toString();
                $('#txtNo').val(model.no);
            }
            if (model.no == '' || model.label == '' || model.parentno == '' || model.level == '') {
                alert('数据不完整');
                return false;
            }
            if (model.v2 == '')
                model.v2 = '/Home/ApiDoc/?parentno=' + model.parentno + '&no=' + model.no;

            //model.v1 = 'http://192.168.60.38:8001/content/document/api/' + model.no + '.html';

            var url = '/Home/SaveTree/';
            $.post(url, model, function (res) {
                if (res == 100) {
                    alert('Success !');
                    location.reload();
                } else {
                    console.log('Save: ' + res.Msg);
                }
            }, 'json');

        }

        function publish() {
            var model = { no: $('#txtno').val(), html: testEditor.getPreviewedHTML() };
            if (model.no == '') {
                alert("请先保存，后发布！");
                return false;
            }
            model.html = encodeURIComponent(model.html);
            var url = '/Doc/Publish/';

            $.post(url, model, function (res) {
                if (res.Code == 100) {
                    $('#a-preview').attr('href', '/content/document/api/' + model.no + '.html').show();
                    alert("Publish success !!!");
                } else {
                    console.log('Save: ' + res.Msg);
                }
            }, 'json');


        }

        function addApiDoc() {
            $('#editortoolbar').hide();
            $('#addApidoc').show();
        }

        function saveApi() {

            $('#msg').text('');
            var model = {};
            model.no = (new Date().getTime()).toString();
            model.level = 2;
            model.ordernum = 0;
            model.parentno = 2000;
            model.type = 2000;
            model.label = $('#txtApiName').val();
            model.remark = $('#txtApiName').val();
            model.v1 = 'code';
            model.v2 = '#';
            model.v3 = '';

            if (model.no == '' || model.label == '' || model.parentno == '' || model.level == '') {
                alert('数据不完整');
                return false;
            }

            var url = '/Home/SaveTree/';
            $.post(url, model, function (res) {
                if (res == 100) {
                    debugger;
                    saveApiItemTemplate(model);
                } else {
                    console.log('Save: ' + res.Msg);
                }
            }, 'json');
        }
        function saveApiItemTemplate(api) {

            var model = {};
            model.no = (new Date().getTime()).toString();
            model.level = 3;
            model.ordernum = 1;
            model.parentno = api.no;
            model.type = 2000;
            model.label = api.label + " 接口模板";
            model.remark = api.label + " 接口模板";
            model.v1 = '';
            model.v2 = '/Home/ApiDoc/?parentno=' + model.parentno + '&no=' + model.no;
            model.v3 = '# ' + api.label + " 接口模板";

            if (model.no == '' || model.label == '' || model.parentno == '' || model.level == '') {
                alert('数据不完整');
                return false;
            }
            
            var url = '/Home/SaveTree/';
            $.post(url, model, function (res) {
                if (res == 100) {
                    alert('Success !');
                    location.reload();
                } else {
                    console.log('Save: ' + res.Msg);
                }
            }, 'json');

        }

    </script>

}